import openpyxl
import os
import logging
import coloredlogs
from logging import debug, info, error


def UseOpenpyxl(file_name):
    wb = openpyxl.load_workbook(file_name, read_only=True)
    sheet = wb.active
    rows = sheet.rows
    first_row = [cell.value for cell in next(rows)]
    data = []
    for row in rows:
        record = {}
        for key, cell in zip(first_row, row):
            if cell.data_type == 's':
                record[key] = cell.value.strip()
            else:
                record[key] = cell.value
        data.append(record)
    return data


def get_img_data():
    list_file = 'info.xlsx'
    data = UseOpenpyxl(list_file)

    img_data = {}
    for row in data:
        # debug(row)
        num_str = row['编号']
        if num_str is None or num_str == '':
            break
        num = int(num_str)
        img_data[num] = {
            'num': num,
            'name': row['姓名'],
            'age': row['年龄'],
            'time': row['调查日期'],
            'diabetes': '1' if row['老糖'] == '1' or row['新糖'] == '1' else '0',
            'high_blood_pressure': '1' if row['高血压'] == '1' or row['新高血压'] == '1' else '0',
            'left_sight': 'not given' if row['左眼远视力'] is None else row['左眼远视力'],
            'right_sight': 'not given' if row['右眼远视力'] is None else row['右眼远视力'],
        }
    # debug(img_data[num])
    return img_data


if __name__ == '__main__':
    coloredlogs.install(level='DEBUG', logger=logging.getLogger(),
                        fmt='%(filename)6s:%(lineno)3d %(message)s',
                        datefmt='%H:%M:%S',
                        )
    get_img_data()
